package com.seirsoft.cookingtime.controller;

import java.util.List;

import com.seirsoft.cookingtime.internal.IViewTable;
import com.seirsoft.cookingtime.internal.IViewTableCallback;
import com.seirsoft.cookingtime.model.TimeElement;
import com.seirsoft.cookingtime.ui.dialogfactory.DialogFactory;
import com.seirsoft.cookingtime.view.IDlgBase;
import com.seirsoft.cookingtime.view.IDlgTimeSelect;
import com.seirsoft.framework.util.Common;

public class CtlDlgTimeSelect extends CtlDlgBase
implements IViewTableCallback 
{
	final private static int TableColumn_Type = 1;
	final private static int TableColumn_Time = 2;
	private List<TimeElement> aryTimeElement = null;
	
	private IViewTable m_tableviewer = null;
	public CtlDlgTimeSelect(IDlgBase dlgBase) {
		super(dlgBase);
		
		aryTimeElement = TimeConfig.getInstance().getTimeElementList();
	}

	protected String getDialogTitle() {
		return Messages.getMessageString("DlgTimeSelect.Title"); //$NON-NLS-1$
	}
	
	protected IDlgTimeSelect getDialog()
	{
		return (IDlgTimeSelect) super.m_dialog;
	}

	@Override
	public void fireEventUICreated() {
		this.getDialog().setTitle(this.getDialogTitle());
		//this.getDialog().setSize(300, 180);
		if(m_tableviewer != null)
		{
			this.m_tableviewer.setTableHeaders(
					new String[]{
							"",
							Messages.getMessageString("DlgTimeSelect.Type"), 
							Messages.getMessageString("DlgTimeSelect.Time")
							},
					new int[]{
							30,
							160,
							160
					}
							
			);
		
			this.m_tableviewer.SetInput(this.aryTimeElement);
		}
	}
	
	private void openDialogTimeSelect(long lSeconds)
	{
		IDlgBase dlgDisplayTime = DialogFactory.getInstance().getDialogDisplayTime(super.m_dialog);
		
		CtlDlgDisplayTime ctlDlgDisplayTime = new CtlDlgDisplayTime(dlgDisplayTime, lSeconds);
		ctlDlgDisplayTime.open();
	}
	

	public void setTimeTable(IViewTable tableviewer) {
		m_tableviewer = tableviewer;
		m_tableviewer.AddSelectionListener(this);
	}

	@Override
	public void fireEventCellTextChanged(int iRow, int iColumn, String text) {
		if(iRow>=0 && iRow < aryTimeElement.size())
		{
			TimeElement objTimeElement = aryTimeElement.get(iRow);
			if(iColumn == TableColumn_Type)
				objTimeElement.setStrName(text);
			else if(iColumn == TableColumn_Time)
			{
				if(Common.isDigital(text))
					objTimeElement.setISecond(Integer.parseInt(text));
			}
			
			if(m_tableviewer != null)
			{
				m_tableviewer.updateRow(iRow, objTimeElement);
			}
		}
	}

	public String fireEventGetColumnText(TimeElement objTimeElement, int columnIndex) 
	{
    	if(columnIndex == TableColumn_Type)
    	{
    		return objTimeElement.getStrName();
    	}
    	else if(columnIndex == TableColumn_Time)
    	{
    		return String.valueOf(objTimeElement.getISecond());
    	}
    	
    	return "";
	}

	@Override
	public void fireEventFocusLost() {
		this.m_tableviewer.SetViewMode();
	}
	
	private int m_iCurrentRow = -1; 
	@Override
	public void fireEventCellClick(boolean isCellClicked, int iRow, int iColumn) {
		this.m_tableviewer.SetViewMode();
		
		if(isCellClicked)
			m_iCurrentRow = iRow;
		else
		{
			m_iCurrentRow = -1;
			this.m_tableviewer.SetSelection(null);
		}
	}


	@Override
	public void fireEventCellDoubleClick(boolean isCellClicked, int iRow,
			int iColumn) {
		if(isCellClicked)
		{
			this.m_tableviewer.EditCell(iRow, iColumn);
		}
		else
		{
			this.m_tableviewer.SetViewMode();
		}
	}

	public void fireEventToolbarClickSave() {
		TimeConfig.getInstance().save(this.aryTimeElement);
	}

	public void fireEventToolbarClickAdd() {
		TimeElement objTimeElement = new TimeElement();
		objTimeElement.setStrName("");
		objTimeElement.setISecond(0);
		this.aryTimeElement.add(objTimeElement);
		
		if(this.m_tableviewer != null)
		{
			this.m_iCurrentRow = -1;
			this.m_tableviewer.SetSelection(null);
			this.m_tableviewer.SetInput(this.aryTimeElement);
			this.m_tableviewer.RefreshTable();
		}
	}

	public void fireEventToolbarClickDelete() {
		if(this.m_iCurrentRow >= 0)
		{
			this.aryTimeElement.remove(this.m_iCurrentRow);
			if(this.m_tableviewer != null)
			{
				this.m_tableviewer.SetInput(this.aryTimeElement);
				this.m_tableviewer.RefreshTable();
			}
			this.m_iCurrentRow = -1;
		}
	}
}
